package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.anno.LogOperation;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/depts")
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;
    @GetMapping
    @LogOperation
    public Result list(){
        return Result.success(deptService.findAll());
    }
    @DeleteMapping
    public Result delete(@RequestParam("id") Integer deptId){
        System.out.println("删除部门id:"+deptId);
        deptService.deleteById(deptId);
        return Result.success();
    }
    @PostMapping
    public Result addDeot(@RequestBody Dept dept){
        System.out.println("新增部门:"+dept);
        deptService.addDept(dept);
        return Result.success();
    }
    @LogOperation
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable("id") Integer id){
        System.out.println("根据id查询部门:"+id);
        Dept dept = deptService.getInfo(id);
        return Result.success(dept);
    }

    @PutMapping
    public Result update(@RequestBody Dept dept){
        System.out.println("修改部门:"+dept);
        deptService.update(dept);
        return Result.success();
    }

}
